Skip to content

Prevent excessive CPU use on corrupt MSI sector chains - #535

Open
mtrojnar wants to merge 1 commit into
masterfrom
fix/msi-sector-chain-cpu
Open

mtrojnar wants to merge 1 commit into
masterfrom
fix/msi-sector-chain-cpu

Conversation

@mtrojnar

Copy link
Copy Markdown
Owner

Pull Request Type

  • Bug fix

Related Issue

Issue number: N/A

Current Behavior

Malformed MSI sector chains can treat ENDOFCHAIN as a sector number, causing millions of unnecessary DIFAT-walk iterations during signing or verification.

New Behavior

Reject chain terminators and FAT sector numbers outside the header's declared range before walking the chain. Stop reading a mini stream once its requested bytes have been copied, avoiding an unnecessary lookup past its final sector.

Scope of Changes

MSI sector-chain validation and its release note.

Testing

  • Existing tests
  • New tests added
  • Manual testing

Release build succeeded with GCC 16.2 and OpenSSL 3.6.5. All 29 selected CTest checks passed: the MSI storage-tree tests and HTTP-server setup/cleanup (ctest -R '^(start_server|stop_server|msi_tree_.*)$' --output-on-failure).

License Declaration

  • I hereby agree to license my contribution under the project's license.

@mtrojnar
mtrojnar requested a review from olszomal November 17, 2026 10:54

@olszomal olszomal left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to [MS-CFB], section 2.1, valid sector numbers range from 0 to MAXREGSECT (0xFFFFFFFA), inclusive.

The proposed condition still rejects sector 0, which is valid, and accepts reserved values such as DIFSECT and FATSECT, which cannot be used as links to the next sector.

I suggest replacing the entire condition with:

if (nextSectorLocation > MAXREGSECT) {

This also fixes the pre-existing rejection of sector 0.

The same validation should be applied consistently in:

  • get_next_sector()
  • get_next_mini_sector()
  • get_fat_sector_location()
  • sector_offset_to_address()
  • mini_sector_offset_to_address()

The last two functions currently use >= MAXREGSECT, incorrectly excluding the maximum valid sector number.

The new FAT sector count check and the change to read_mini_stream() look correct.

Reference: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-cfb/9d33df18-7aee-4065-9121-4eabe41c29d4

@olszomal olszomal left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All my previous comments have been addressed. Sector number validation is now consistent with MS-CFB across all five functions.

I also appreciate the new regression test covering backward links to sector 0 in both FAT and MiniFAT chains and checking stream preservation.

Looks good to me.

Reject reserved sector markers when a chain requires another sector, and
bound FAT lookups by the header's declared sector count. This avoids
excessive CPU use on malformed MSI files while allowing valid links to
sector zero. Stop reading a mini stream once its requested bytes have
been copied so a valid end-of-chain marker is not mistaken for an error.

Test that signing and removing signatures preserve stream data when FAT
and MiniFAT chains link back to sector zero.
@mtrojnar
mtrojnar force-pushed the fix/msi-sector-chain-cpu branch from c8bb25a to c67e337 Compare December 4, 2026 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants